package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.config.keyword.DateUtils;
import com.cskaoyan.catmall.dao.MarketIssueMapper;
import com.cskaoyan.catmall.model.bo.PageBo;
import com.cskaoyan.catmall.model.pojo.MarketIssue;
import com.cskaoyan.catmall.model.vo.issue.IssueListVo;
import com.cskaoyan.catmall.model.vo.issue.IssueVo;
import com.cskaoyan.catmall.model.vo.keyword.GetListVo;
import com.cskaoyan.catmall.model.vo.keyword.KeywordVo;
import com.cskaoyan.catmall.service.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/01/08 11:22
 *
 * @author WangZhe
 * 备注:
 */
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public IssueListVo getListByQuestion(PageBo pageBo, String question) {
        Integer page = pageBo.getPage();//页码
        Integer limit = pageBo.getLimit();//数据量
        String sort = pageBo.getSort();//排序列
        String order = pageBo.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);

        List<IssueVo> list = issueMapper.getListByQuestion(sort, order, question);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<IssueVo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return IssueListVo.data(total, pages, limit, page, list);
    }

    @Override
    public IssueVo createIssue(MarketIssue marketIssue) {
        marketIssue.setAddTime(DateUtils.formatDate(new Date()));
        marketIssue.setUpdateTime(DateUtils.formatDate(new Date()));
        issueMapper.createIssue(marketIssue);
        IssueVo issueVo = issueMapper.selectOneById(marketIssue.getId());

        return issueVo;
    }

    @Override
    public void deleteIssue(Integer id) {
        issueMapper.deleteIssue(id);
    }

    @Override
    public void updateIssue(MarketIssue marketIssue) {
        marketIssue.setUpdateTime(DateUtils.formatDate(new Date()));
        issueMapper.updateIssue(marketIssue);
    }

}
